package cn.hjh.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.hjh.pojo.Emp;
import cn.hjh.pojo.EmpCondition;
import org.hibernate.Query;

import javax.persistence.Table;


@Table(name="Emp")
public class EmpDao extends BaseDao {
	
	public List<Emp> findTopSalary(int topSize) {
		return this.currentSession()
				.createQuery("from Emp where salary is not null order by salary desc")
				.setMaxResults(topSize)
				.list();
	}
	
	public List<Emp> findByPage(int pageNo, int pageSize) {
		return this.currentSession().createQuery("from Emp order by id")
				.setFirstResult((pageNo - 1) * pageSize)	//设置获取结果的起始下标
				.setMaxResults(pageSize)	//设置最大返回结果数
				.list();
	}
	
	public List<Emp> findByConditions(String hql, Map<String, Object> conditions) {
		Query query = this.currentSession().createQuery(hql);
		query.setProperties(conditions);
		return query.list();
	}

	public Long ob(Double sal){
		String hql  = "select count(id) from Emp where salary >= :sal";
		return (Long) HibernateUtil.currentSession().createQuery(hql).setDouble("sal",sal).uniqueResult();
	}

	public List<Emp> findByConditions(String hql, EmpCondition conditions) {
		Query query = this.currentSession().createQuery(hql);
		query.setProperties(conditions);
		return query.list();
	}
	
	public List<Emp> findByConditions(Emp conditions) {
		Query query = this.currentSession().createQuery("from Emp where job = :job and salary > :salary");
		query.setProperties(conditions);
		return query.list();
	}
	
	public List<Emp> findByConditions(Object[] conditions) {
		Query query = this.currentSession().createQuery("from Emp where job = ? and salary > ?");
		if (conditions != null && conditions.length != 0) {
			for (int i = 0; i < conditions.length; ++i) {
				query.setParameter(i, conditions[i]);
			}
		}
		return query.list();
	}
	
	public List<Emp> findAllEmp() {
		String hql = "from Emp";
		Query query = HibernateUtil.currentSession().createQuery(hql);
		return query.list();
	}

	public Iterator<Emp> findAllEmp_iterate() {
		return this.currentSession().createQuery("from Emp").iterate();
	}
	
	public Long countEmp() {
		return (Long) this.currentSession().createQuery("select count(id) from Emp")
				.uniqueResult();
	}
	
	public Emp countEmp_2() {
		return (Emp) this.currentSession().createQuery("from Emp")
				.uniqueResult();
	}
	
}
